﻿package nl.haemers.preloader 
{
	import flash.display.Sprite;
	import flash.events.Event;
	/**
	 * ...
	 * @author Jonathan Haemers
	 */
	public class Preloader extends Sprite
	{
		private var _loaded				:Number;
		private var _total				:Number;
		private var _perc				:Number;
		
		public function Preloader() 
		{
			if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		/** Public methods **/
		
		/** Getters & Setters **/
		public function set loaded( value:Number ) : void
		{
			_loaded = value;
			calcPerc();
			check();
		}
		
		public function set total( value:Number ) : void
		{
			_total = value;
		}
		
		public function set perc( value:Number ) : void
		{
			_perc = value;
		}
		
		/** Protected methods **/
		protected function check() : void
		{
			if (_loaded >= _total)
			{
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		protected function update() : void
		{
			//update view
		}
		
		/** Private methods **/
		private function init( e:Event = null ) : void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
		}
		
		private function calcPerc() : void
		{
			_perc = (_loaded / _total) * 100;
		}
		
	}

}